package com.eastjet.maintenance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.maintenance.domain.Application;
import com.eastjet.maintenance.domain.DeviceAudits;
import com.eastjet.maintenance.domain.Documentation;
import com.eastjet.maintenance.domain.Purchasedetails;
import com.eastjet.maintenance.feign.CodeDataFeing;
import com.eastjet.maintenance.mapper.ApplicationMapper;
import com.eastjet.maintenance.service.ApplicationService;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 杨博文
 * @description 针对表【application(采购申请表)】的数据库操作Service实现
 * @createDate 2024-11-25 09:23:37
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application> implements ApplicationService {

    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private CodeDataFeing codeDataFeing;
    @Autowired
    private PurchasedetailsServiceImpl purchasedetailsService;//添加采购设备明细
    @Autowired
    private DeviceAuditsServiceImpl deviceAuditsService;//添加设备审核记录
    @Autowired
    private DocumentationServiceImpl documentationServiceImpl;//注入文档Service

    /*
    新增采购申请/新增采购明细
     */
    @SneakyThrows
    @Override
    public Boolean InsertApplication(Map<String, Object> map) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date datee = df.parse(df.format(new Date()));
        String code = codeDataFeing.test(new HashMap<String, String>() {{
            put("TableName", "application");
        }});
        Application app = new Application();
        app.setCode(code);
        app.setTopic(map.get("topic").toString());
        app.setApplicant(map.get("applicant").toString());
        app.setDepartment(map.get("department").toString());
        app.setContact(map.get("contact").toString());
        app.setAffair(map.get("affair").toString());
        app.setState(1);
        app.setPan(1);
        app.setRemark(map.get("remark").toString());
        app.setQuantity(Integer.valueOf(String.valueOf(map.get("quantity"))));
        app.setAmount(Double.valueOf(String.valueOf(map.get("amount"))));
        app.setIs_delete(0);
        app.setCreatedBy(map.get("usname").toString());
        boolean save = this.save(app);
        if (save) {
            List<Object> tableData = (List<Object>) map.get("tableData");
            List<Purchasedetails> purchasedetailsList = new ArrayList<>();
            tableData.forEach(item -> {
                item = (Map<String, Object>) item;
                Purchasedetails p = new Purchasedetails();
                p.setNumbering(((Map<?, ?>) item).get("numbering").toString());
                p.setName(((Map<?, ?>) item).get("name").toString());
                p.setModel(((Map<?, ?>) item).get("model").toString());
                p.setType(((Map<?, ?>) item).get("type").toString());
                p.setBrand(((Map<?, ?>) item).get("brand").toString());
                p.setReference(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item).get("reference").toString())));
                p.setQuantity(Integer.valueOf(String.valueOf(((Map<?, ?>) item).get("quantity"))));
                p.setTombstone(0);
                p.setEstimate(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item).get("reference").toString()) * Integer.valueOf(String.valueOf(((Map<?, ?>) item).get("quantity")))));
                p.setRequestNumber(code);
                p.setCreatedBy(map.get("usname").toString());
                purchasedetailsList.add(p);
            });
            Boolean b = purchasedetailsService.InsertPurchasedetails(purchasedetailsList);
            if (b) {
//                DeviceAudits de = new DeviceAudits();
//                de.setCode(code);
//                de.setAuditsTitle("设备采购申请");
//                de.setAuditsRange("admin");
//                de.setAuditsProcess("发起人->" + map.get("username").toString() + "->审批人->admin");
//                return deviceAuditsService.InsertAudits(de);
                //新增文档
                List<Object> documentationData = (List<Object>) map.get("documents");
                List<Documentation> documentationList = new ArrayList<>();
                //循环映射文档数据
                documentationData.forEach(item -> {
                    item = (Map<String, Object>) item;
                    Documentation doc = new Documentation();
                    doc.setCode(code);
                    doc.setFilename(((Map<?, ?>) item).get("name").toString());
                    doc.setUploadTime(datee);
                    doc.setUploader(map.get("usname").toString());
                    doc.setSize(((Map<?, ?>) item).get("size").toString());
                    doc.setEid(1);
                    doc.setCreationTime(datee);
                    doc.setCreatedBy(map.get("usname").toString());
                    documentationList.add(doc);
                });
                return documentationServiceImpl.InsertList(documentationList);
            }
        }
        return false;
    }

    /*
    新增备件采购申请/新增采购明细
     */
    @SneakyThrows
    @Override
    public Boolean InsertApplicationBei(Map<String, Object> map) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date datee = df.parse(df.format(new Date()));
        String code = codeDataFeing.test(new HashMap<String, String>() {{
            put("TableName", "application");
        }});
        Application app = new Application();
        app.setCode(code);
        app.setTopic(map.get("topic").toString());
        app.setApplicant(map.get("applicant").toString());
        app.setDepartment(map.get("department").toString());
        app.setContact(map.get("contact").toString());
        app.setAffair(map.get("affair").toString());
        app.setState(1);
//        app.setUnit(map.get("unit").toString());
        app.setPan(2);
        app.setRemark(map.get("remark").toString());
        app.setQuantity(Integer.valueOf(String.valueOf(map.get("quantity"))));
        app.setAmount(Double.valueOf(String.valueOf(map.get("amount"))));
        app.setIs_delete(0);
        app.setCreatedBy(map.get("usname").toString());
        boolean save = this.save(app);
        if (save) {
            List<Object> tableData = (List<Object>) map.get("tableData");
            List<Purchasedetails> purchasedetailsList = new ArrayList<>();
            tableData.forEach(item -> {
                item = (Map<String, Object>) item;
                Purchasedetails p = new Purchasedetails();
                p.setNumbering(((Map<?, ?>) item).get("code").toString());
                p.setName(((Map<?, ?>) item).get("name").toString());
                p.setModel(((Map<?, ?>) item).get("specification").toString());
                p.setType(((Map<?, ?>) item).get("type").toString());
                p.setBrand(((Map<?, ?>) item).get("brand").toString());
                p.setReference(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item).get("reference").toString())));
                p.setQuantity(Integer.valueOf(String.valueOf(((Map<?, ?>) item).get("quantity"))));
                p.setTombstone(0);
                p.setEstimate(BigDecimal.valueOf(Double.valueOf(((Map<?, ?>) item).get("reference").toString()) * Integer.valueOf(String.valueOf(((Map<?, ?>) item).get("quantity")))));
                p.setRequestNumber(code);
                p.setCreatedBy(map.get("usname").toString());
                purchasedetailsList.add(p);
            });
            Boolean b = purchasedetailsService.InsertPurchasedetails(purchasedetailsList);
            if (b) {
                //新增文档
                List<Object> documentationData = (List<Object>) map.get("documents");
                List<Documentation> documentationList = new ArrayList<>();
                //循环映射文档数据
                documentationData.forEach(item -> {
                    item = (Map<String, Object>) item;
                    Documentation doc = new Documentation();
                    doc.setCode(code);
                    doc.setFilename(((Map<?, ?>) item).get("name").toString());
                    doc.setUploadTime(datee);
                    doc.setUploader(map.get("usname").toString());
                    doc.setSize(((Map<?, ?>) item).get("size").toString());
                    doc.setEid(1);
                    doc.setCreationTime(datee);
                    doc.setCreatedBy(map.get("usname").toString());
                    documentationList.add(doc);
                });
                return documentationServiceImpl.InsertList(documentationList);
            }
        }
        return false;
    }

    /*
    修改采购申请
     */
    @Override
    public Boolean UpdateApplication(Application application) {
        return this.updateById(application);
    }

    /*
    删除采购申请
     */
    @Override
    public Boolean DeleteApplication(Application code) {
        return this.updateById(code);
    }

    /*
    查询所有采购申请
     */
    @Override
    public List<Application> selectAllApplication(Application application) {
        return applicationMapper.selectAllApplication(application);
    }

    /**
     * 查询所有已经审核的采购申请
     *
     * @param application
     * @return
     */
    @Override
    public List<Application> selectAllApplicationQi(String status) {
        return this.list(new QueryWrapper<Application>().and(i -> i.eq("state", status).eq("is_delete", 0)));
    }

    /**
     * 修改采购申请的状态
     *
     * @param code
     * @param status
     * @return
     */
    @Override
    public Boolean UpdteApplicationQi(String code, String status) {
        return applicationMapper.UpdteApplicationQi(code, status) == 1 ? true : false;
    }
}




